package org.example.diet_compass.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.example.diet_compass.domain.Cart;
import org.example.diet_compass.dto.CartItemDto;
import org.example.diet_compass.dto.CartParam;
import org.example.diet_compass.resp.Result;
import org.example.diet_compass.resp.ResultCodeEnum;
import org.example.diet_compass.service.CartService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Archer_hui
 * @version 1.0
 * @Date 2025/3/16 10:34
 */
@Tag(name = "购物车控制类", description = "用于处理购物车相关功能的api")
@RestController
@RequestMapping("/cart")
public class CartController {
    @Resource
    CartService cartService;

    @PostMapping("/add-good-to-cart")
    @Operation(summary = "添加商品到购物车")
    @Transactional
    public Result<Void> addGoodToCart(@RequestBody CartParam cartItemDto) {
        String goodId = cartItemDto.getGoodId();
        int userId = cartItemDto.getUserId();
        Cart good = cartService.getCartGoodByUserIdAndGoodId(userId, goodId);
        if (good != null) {return Result.build(null, ResultCodeEnum.PRODUCT_CART_REPECT);}
        Integer count = cartItemDto.getCount();
        Cart cart = new Cart(null, userId, goodId, 1, count);
        cartService.saveOrUpdate(cart);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/get-cart-goods/{userId}")
    @Operation(summary = "根据用户id获取购物车的内容")
    public Result<List<CartItemDto>> getCart(@PathVariable("userId") Integer userId) {
        //获取购物车id
        List<CartItemDto> cartItemList = cartService.getGoodsByUserId(userId);
        return Result.build(cartItemList, ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/delete-cart-goods/{userId}")
    @Operation(summary = "购物车删除api")
    public Result<Void> deleteCartGoods(@PathVariable("userId") Integer userId, @RequestBody List<String> goodIdList) {
        goodIdList.forEach(goodId -> {
            cartService.removeByUserIdAndgoodId(userId, goodId);});
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PutMapping("/update-cart-good")
    @Operation(summary = "更新购物车指定商品的api")
    public Result<Void> updateCartGood(@RequestBody CartParam cartItemDto) {
        int count = cartItemDto.getCount();
        Boolean selected = cartItemDto.getSelected();
        String goodId = cartItemDto.getGoodId();
        int userId = cartItemDto.getUserId();
        cartService.updateGood(userId,goodId,count,selected);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PutMapping("/cart-select-all/{selected}/{userId}")
    @Operation(summary = "购物车商品全选")
    public Result<Void> cartSelectAll(@PathVariable("selected") Boolean selected,@PathVariable Integer userId) {
        cartService.updateAllGoods(selected, userId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}

